package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.cz.nongyetongv5.R;

public class MapViewActivity extends Activity implements View.OnClickListener {
    private Double latitude = 0.00;
    private Double longitude = 0.00;
    private MapView mapView;
    private BaiduMap mBaiduMap;
    private RelativeLayout btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_view);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        mapView=findViewById(R.id.mapView);
        latitude = getIntent().getDoubleExtra("latitude", 0.0d);
        longitude = getIntent().getDoubleExtra("longitude", 0.0d);
        mBaiduMap = mapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
        mBaiduMap.setMapStatus(msu);
        //        addOverlay(latitude, longitude);
        initOverlay();
    }

    public void initOverlay() {
        // add marker overlay
        LatLng llA = new LatLng(latitude, longitude);
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);

        MarkerOptions ooA = new MarkerOptions().position(llA).icon(bitmap)
                .zIndex(9).draggable(true);
        // 掉下动画
        ooA.animateType(MarkerOptions.MarkerAnimateType.drop);

        Marker mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));

        MapStatusUpdate u = MapStatusUpdateFactory
                .newLatLng(llA);
        mBaiduMap.setMapStatus(u);
        //        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
        //            @Override
        //            public boolean onMarkerClick(Marker marker) {
        //                TextView tv = new TextView(MapViewActivity.this);
        //                tv.setBackgroundColor(Color.GRAY);
        //                tv.setPadding(20, 10, 20, 20);
        //                tv.setGravity(Gravity.CENTER);
        //                tv.setTextColor(android.graphics.Color.WHITE);
        //                tv.setText(address);
        //                final LatLng latLng1 = new LatLng(startLatitude, startLongitude);
        //                final LatLng latLng = new LatLng(latitude, longitude);
        //                mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(tv), latLng, -47, new InfoWindow.OnInfoWindowClickListener() {
        //                    @Override
        //                    public void onInfoWindowClick() {
        //
        //                        mBaiduMap.hideInfoWindow();
        //
        //                        CustomDialog.Builder builder = new CustomDialog.Builder(MapViewActivity.this);
        //                        builder.setMessage("是否启动导航？");
        //                        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
        //                            @Override
        //                            public void onClick(DialogInterface dialog, int which) {
        //                                dialog.dismiss();
        //                                startNavi(latLng1, latLng);
        //                            }
        //                        });
        //                        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
        //                            @Override
        //                            public void onClick(DialogInterface dialog, int which) {
        //                                dialog.dismiss();
        //                            }
        //                        });
        //
        //                        builder.createNormalDialog().show();
        //                    }
        //                });
        //                mBaiduMap.showInfoWindow(mInfoWindow);
        //                return true;
        //            }
        //        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
        }
    }
}
